package com.milestone.fitnesspersonaltrainer.network.rx;

import android.util.Log;

import com.milestone.fitnesspersonaltrainer.network.exception.ResponseFailedException;
import com.milestone.fitnesspersonaltrainer.network.response.BaseResponse;
import com.milestone.fitnesspersonaltrainer.network.response.ErrorCode;
import com.milestone.fitnesspersonaltrainer.network.response.ResponseCode;
import com.milestone.fitnesspersonaltrainer.network.response.RxVoid;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * Created by shendawei on 17/2/8.
 */

public class BaseResponseFunc1<T> implements Function<BaseResponse<T>, Flowable<T>> {

    @Override
    public Flowable<T> apply(@NonNull final BaseResponse<T> response) throws Exception {

        if (response == null) {
            return Flowable.error(new ResponseFailedException(ErrorCode.NETWORK_ERROR,"网络出现问题1"));
        }
        Log.d("BaseResponse", "response = " + response);

        /*
         */
        if(response.getStatus()== ResponseCode.NON_LOGIN)
        {
//            RepositoryFactory.getLoginContext(NeoApplication.getContext()).logout();
        }

        if(response.getStatus()==ResponseCode.SUCCESS&&response.getContent()==null)
        {
            return Flowable.just((T) new RxVoid());
        }
        else if(response.getStatus()==ResponseCode.SUCCESS&&response.getContent()!=null)
        {
            return Flowable.just(response.getContent());
        }
        else if(response.getStatus()!=ResponseCode.SUCCESS&&response.getContent()==null)
        {
            return Flowable.error(new ResponseFailedException(response.getStatus(),response.getMessage()));
        }
        else if(response.getStatus()!=ResponseCode.SUCCESS&&response.getContent()!=null)
        {
            return Flowable.error(new ResponseFailedException(response.getStatus(),response.getMessage(),response.getContent()));
        }
        return Flowable.error(new ResponseFailedException(ErrorCode.NETWORK_ERROR,"connect time out"));




      /*  // log code
        Log.d("BaseResponse", "response.code = " + response.getCode());
        if(response.getCode()== NeoConstantCode.sys_code_img_valid
                ||response.getCode()==NeoConstantCode.sys_code_later)
        {
            //Log.v("BaseResponse",((Send)response.getData()).getCaptcha());
            if(response.getData() instanceof Send) {
                Send send = (Send) response.getData();
                send.setCode(response.getCode());
                return Flowable.just((T) send);
            }
        }

        // 1.code == success 2.code != success, but data != null
        if (response.getCode() == ResponseCode.SUCCESS || response.getData() != null) {
            if(response.getData()==null)
            {
                return Flowable.just((T)new RxVoid());
            }
            return Flowable.just(response.getData());
        }
        else { //3.code != success and data == null
            return Flowable.error(new ResponseFailedException(response.getCode()));
        }*/
    }
}
